//
//  LoadModifer.swift
//  DocScaner
//
//  Created by liyahao on 2025/8/18.
//

import SwiftUI

struct LoadModifer<T: View>: ViewModifier {
    @Binding var loading: Bool
    @Binding var reLoading: Bool
    @Binding var empty: Bool
    let view: T?
    
    func body(content: Content) -> some View {
        ZStack {
            content
            if empty {
                VStack(spacing: 0) {
                    Image(systemName: "wifi.exclamationmark")
                        .resizable()
                        .scaledToFit()
                        .foregroundColor(Color(.black))
                        .frame(width: 90, height: 90)
                    
                    Button {
                        reLoading.toggle()
                    } label: {
                        Text("刷新")
                            .font(.system(size: 12, weight: .regular))
                            .foregroundColor(.black)
                            .padding(.horizontal, 20)
                            .padding(.vertical, 5)
                            .background(.white)
                            .cornerRadius(10)
                    }
                }
            }
            
            if loading, let contentV = view {
                contentV
            }
        }
        .ignoresSafeArea(.all)
    }
}

extension View {
    func loading<T: View>(empty: Binding<Bool>,
                            load: Binding<Bool>,
                            reLoad: Binding<Bool> = .constant(true),
                            view: T?) -> some View {
        return self.modifier(LoadModifer(loading: load, reLoading: reLoad, empty: empty, view: view))
    }
}
